package com.qf.blog.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.blog.commons.constants.SysConsts;
import com.qf.blog.commons.security.UserTokenThreadLocal;
import com.qf.blog.entity.Letter;
import com.qf.blog.entity.User;
import com.qf.blog.mapper.LetterMapper;
import com.qf.blog.service.ILetterService;
import com.qf.blog.service.IUserService;
import com.qf.blog.vo.LetterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class LetterServiceImpl extends ServiceImpl<LetterMapper, Letter> implements ILetterService {
     @Autowired
     private IUserService iUserService;

    @Override
    public List<LetterVo> queryById(String id) {

        return baseMapper.queryById(id);
    }

    @Override
    public List<LetterVo> queryByUserId(Integer id) {
        return baseMapper.queryByUserId(id);
    }

    @Override
    public List<LetterVo> updateStatus(Integer id) {
        Letter letter=baseMapper.selectById(id);
        letter.setStatus(SysConsts.MesaaggeStatus.READED);
        baseMapper.updateById(letter);
        String conversationId=letter.getConversationId();
        return baseMapper.queryById(conversationId);
    }

    @Override
    public void add(String toName, String content) {
        User toUser=iUserService.getUserByColumn("username", toName);
        Integer toId=toUser.getId();
        User fromUser=UserTokenThreadLocal.getUser();
        Integer fromId=fromUser.getId();
        Letter letter=new Letter();
        letter.setContent(content);
        letter.setToId(toId);
        letter.setFromId(fromId);
        letter.setConversationId(fromId+""+toId);
        baseMapper.insert(letter);
    }
}
